import { ref } from 'vue'
import Configuration from '@/api/client/configuration.js'
import message from 'ant-design-vue/lib/message/index.js'
import { SAVED_SUCCESSFULLY } from '@/constants/message.js'

export function useAllowDownloadInvoiceFile(id) {
  const useAllowDownloadInvoiceFileLoading = ref(true)
  const useAllowDownloadInvoiceFileEnabled = ref(false)

  const getAllowDownloadInvoiceFileStatus = () => {
    Configuration.getAllowDownloadInvoiceFileStatus(id)
      .then(({ data }) => {
        useAllowDownloadInvoiceFileEnabled.value = typeof data === 'boolean' ? data : false
      })
      .finally(() => {
        useAllowDownloadInvoiceFileLoading.value = false
      })
  }

  getAllowDownloadInvoiceFileStatus()
  const handleChangeAllowDownloadInvoiceFile = () => {
    Configuration.changeAllowDownloadInvoiceFileStatus(id).then(() => {
      message.success(SAVED_SUCCESSFULLY)
      getAllowDownloadInvoiceFileStatus()
    })
  }
  return {
    useAllowDownloadInvoiceFileLoading,
    useAllowDownloadInvoiceFileEnabled,
    handleChangeAllowDownloadInvoiceFile
  }
}
